﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Yugi.Client.Card;
using System.Threading;


namespace ServerYuGi
{
    public partial class SSupervisor
    {

        public void Init()
        {
            for (int i = 0; i < ServerSocket.SocketClients.Count; i++)
            {
                SPlayer player = new SPlayer();
                player.ID = i;
                player.SoDiem = Game.SoDiemBatDau;
                Players.Add(player);
            }
        }
        public void BatDauVan()
        {
            Init();          
            Game.isPlaying = true;         
            TurnManager.IDPlayerTrongLuot = Players[0].ID;
            //Players[0].MySupervisor.ReciveMessageFromServer(YMSG.GAME_BEGIN, Players[0].ID, Players[1].ID);
            //Players[1].MySupervisor.ReciveMessageFromServer(YMSG.GAME_BEGIN, Players[1].ID, Players[0].ID);
            ServerSocket.SendMessageToID(Players[0].ID, YMSG.GAME_BEGIN, Players[0].ID,Players[1].ID);
            ServerSocket.SendMessageToID(Players[1].ID, YMSG.GAME_BEGIN, Players[1].ID, Players[0].ID);            
            ChiaBai();
        }       
        void ParallelChiaBai(SPlayer player)
        {            
                int tmp = 0;
                List<YBaseCard> listcard = new List<YBaseCard>();
                List<byte> param = new List<byte>(); /////////////
                for (int i = 1; i <= Game.SoLaChiaBanDau; i++)
                {
                    tmp = random.Next(0, player.SoBaiChuaChia.Count);
                  
                    SCard card = player.SoBaiChuaChia[tmp];
                    card.MyYBaseCard.ID = DemID;
                   // card.MyYBaseCard.Card_TypeID = tmp;
                    param.Add((byte)card.MyYBaseCard.Card_TypeID);
                    param.Add((byte)card.MyYBaseCard.ID);
                    listcard.Add(card.MyYBaseCard);
                    DemID++;
                    player.SoBaiTrenTay.Add(card);
                    player.SoBaiChuaChia.Remove(card);                 
                }	   
                //player.MySupervisor.ReciveMessageFromServer(YMSG.GAME_CHIABAI,listcard.ToArray() );
                ServerSocket.SendMessageToID(player.ID, YMSG.GAME_CHIABAI, param.ToArray());
                
            
        }
        void ChiaBai()
        {
            foreach (SPlayer item in Players)
            {
                ParallelChiaBai(item);
                
            }                   
        }
        
    }
}
